package com.aliwo.algorithm.easy;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.util.Calendar;
import java.util.Date;

import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoUnit.HOURS;

/**
 * .
 *
 * @author Xu Yayuan
 * @date 2022/3/11
 */
public class DemoCalendar {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDate now = LocalDate.now();
        // System.out.println(localDateTime.with(firstHourOfDay()));
        // System.out.println(localDateTime.with(lastHourOfDay()));

        boolean flag = belongCalendar(now +" 8:00:00","2022-03-14 23:59:59","YYYY-mm-dd HH:mm:ss");
        System.out.println(flag);
    }

    /**
     * 判断当前时间是否在时间段内
     * @param beginTimeStr 起始时间，格式应与format匹配
     * @param endTimeStr 结束时间，格式应与format匹配
     * @param format 时间格式，如HH:mm:ss
     * @return boolean
     */
    public static boolean belongCalendar(String beginTimeStr, String endTimeStr, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date nowTime = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            nowTime = df.parse(df.format(new Date()));
            beginTime = df.parse(beginTimeStr);
            endTime = df.parse(endTimeStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }























    public static String judge(Date date1, Date date2, Date startTime, Date endDate) {
        if(date1 == null || date2 == null || startTime == null || endDate == null){
            return "有的数据指空，无法计算。";
        }

        long d1 = date1.getTime();
        long d2 = date2.getTime();
        long v = d2-d1;
        long start = startTime.getTime();
        long end = endDate.getTime();
        if (((d1 - start) >0) && ((end - d2) >0)){
            return "date1-date2 在起始至结束的区间内";
        } else if ( v > 7200000){//2小时等于 7200000毫秒
            return "date1-date2 不在起始至结束的区间内，但 这个时间段大于2个小时 ";
        }
        return "date1-date2 不在起始至结束的区间内，并且 这个时间段小于2个小时";
    }

    /**
     * 天的开始小时
     */
    public static TemporalAdjuster firstHourOfDay() {
        if (Boolean.FALSE) {
            return (temporal) -> temporal.with(HOUR_OF_DAY, 0);
        } else {
            TemporalAdjuster ti = (temporal) -> temporal.with(HOUR_OF_DAY, 0)
                    .plus(+8, HOURS);
            return ti;
        }
    }

    /**
     * 天的结束小时
     */
    public static TemporalAdjuster lastHourOfDay() {
        if (Boolean.FALSE) {
            return (temporal) -> temporal.with(HOUR_OF_DAY, temporal.range(HOUR_OF_DAY).getMaximum());
        } else {
            return (temporal) -> temporal.with(HOUR_OF_DAY, temporal.range(HOUR_OF_DAY).getMaximum())
                    .plus(+8, HOURS);
        }
    }
}
